unit Render.Process.Handler;

interface

{$I cef.inc}

uses Winapi.Windows, System.Classes, System.SysUtils, uCEFInterfaces, uCEFTypes;

type
  TProcessHandler = class
  private
    class var
      FInstance: TProcessHandler;

  public
    /// <summary>
    /// Called after the render process main thread has been created. |extra_info|
    /// is a read-only value originating from
    /// CefBrowserProcessHandler::OnRenderProcessThreadCreated(). Do not keep a
    // reference to |extra_info| outside of this method.
    /// </summary>
    procedure RenderThreadCreatedCallBack(const extraInfo: ICefListValue);
    /// <summary>
    /// Called after WebKit has been initialized.
    /// </summary>
    procedure WebKitInitializedEventCallback;
    /// <summary>
    /// Called after a browser has been created. When browsing cross-origin a new
    /// browser will be created before the old browser with the same identifier is
    /// destroyed.
    /// </summary>
    procedure BrowserCreatedEventCallBack(const browser: ICefBrowser);
    /// <summary>
    /// Called before a browser is destroyed.
    /// </summary>
    procedure BrowserDestroyedEventCallBack(const browser: ICefBrowser);
    /// <summary>
    /// Called before browser navigation. Return true to cancel the navigation or
    /// false to allow the navigation to proceed. The |request| object cannot be
    /// modified in this callback.
    /// </summary>
    procedure BeforeNavigationEventCallBack(const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest;
      navigationType: TCefNavigationType; isRedirect: Boolean; var aStopNavigation: Boolean);
    /// <summary>
    /// Called immediately after the V8 context for a frame has been created. To
    /// retrieve the JavaScript 'window' object use the CefV8Context::GetGlobal()
    /// method. V8 handles can only be accessed from the thread on which they are
    /// created. A task runner for posting tasks on the associated thread can be
    /// retrieved via the CefV8Context::GetTaskRunner() method.
    /// </summary>
    procedure ContextCreatedEventCallBack(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context);
    /// <summary>
    /// Called immediately before the V8 context for a frame is released. No
    /// references to the context should be kept after this method is called.
    /// </summary>
    procedure ContextReleasedEventCallBack(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context);
    /// <summary>
    /// Called for global uncaught exceptions in a frame. Execution of this
    /// callback is disabled by default. To enable set
    /// CefSettings.uncaught_exception_stack_size > 0.
    /// </summary>
    procedure UncaughtExceptionEventCallBack(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context;
      const exception: ICefV8Exception; const stackTrace: ICefV8StackTrace);
    /// <summary>
    /// Called when a new node in the the browser gets focus. The |node| value may
    /// be empty if no specific node has gained focus. The node object passed to
    /// this method represents a snapshot of the DOM at the time this method is
    /// executed. DOM objects are only valid for the scope of this method. Do not
    /// keep references to or attempt to access any DOM objects outside the scope
    /// of this method.
    /// </summary>
    procedure FocusedNodeChangedEventCallBack(const browser: ICefBrowser; const frame: ICefFrame; const node: ICefDomNode);
    /// <summary>
    /// Called when a new message is received from a different process. Return true
    /// if the message was handled or false otherwise. Do not keep a reference to
    /// or attempt to access the message outside of this callback.
    /// </summary>
    procedure ProcessMessageReceivedEventCallBack(const browser: ICefBrowser; sourceProcess: TCefProcessId;
      const message: ICefProcessMessage; var aHandled: Boolean);

  public
    class function GetInstance: TProcessHandler;
  end;


implementation

{ TProcessHandler }

procedure TProcessHandler.BeforeNavigationEventCallBack(const browser: ICefBrowser; const frame: ICefFrame;
  const request: ICefRequest; navigationType: TCefNavigationType; isRedirect: Boolean; var aStopNavigation: Boolean);
begin
  OutputDebugString(PChar('BeforeNavigationEventCallBack'));
end;

procedure TProcessHandler.BrowserCreatedEventCallBack(const browser: ICefBrowser);
begin
  OutputDebugString(PChar('BrowserCreatedEventCallBack.'));
end;

procedure TProcessHandler.BrowserDestroyedEventCallBack(const browser: ICefBrowser);
begin
  OutputDebugString(PChar('BrowserDestroyedEventCallBack.'));
end;

procedure TProcessHandler.ContextCreatedEventCallBack(const browser: ICefBrowser; const frame: ICefFrame;
  const context: ICefv8Context);
begin
  outputdebugstring(PChar(frame.Url));
end;

procedure TProcessHandler.ContextReleasedEventCallBack(const browser: ICefBrowser; const frame: ICefFrame;
  const context: ICefv8Context);
begin
  OutputDebugString(PChar('ContextReleasedEventCallBack.'));
end;

procedure TProcessHandler.FocusedNodeChangedEventCallBack(const browser: ICefBrowser; const frame: ICefFrame;
  const node: ICefDomNode);
begin
  OutputDebugString(PChar('FocusedNodeChangedEventCallBack'));
end;

class function TProcessHandler.GetInstance: TProcessHandler;
begin
  if FInstance = nil then
    FInstance := TProcessHandler.Create;
  Result := FInstance;
end;

procedure TProcessHandler.RenderThreadCreatedCallBack(const extraInfo: ICefListValue);
begin
  OutputDebugString(PChar('RenderThreadCreatedCallBack'));
end;

procedure TProcessHandler.ProcessMessageReceivedEventCallBack(const browser: ICefBrowser; sourceProcess: TCefProcessId;
  const message: ICefProcessMessage; var aHandled: Boolean);
begin
  outputdebugstring(PChar(message.Name))
end;

procedure TProcessHandler.UncaughtExceptionEventCallBack(const browser: ICefBrowser; const frame: ICefFrame;
  const context: ICefv8Context; const exception: ICefV8Exception; const stackTrace: ICefV8StackTrace);
begin
  OutputDebugString(PChar('UncaughtExceptionEventCallBack.'));
end;

procedure TProcessHandler.WebKitInitializedEventCallback;
begin
  OutputDebugString(PChar('WebKitInitializedEventCallback'));
end;

initialization
finalization
  FreeAndNil(TProcessHandler.FInstance);

end.
